AddCSLuaFile( "cl_init.lua" ) -- Make sure clientside
AddCSLuaFile( "shared.lua" )  -- and shared scripts are sent.
 
include('shared.lua')
 
function ENT:Initialize()
 	util.AddNetworkString("openclHitMenu")
 	util.AddNetworkString("placeHit")
 	util.AddNetworkString("updateHitList")
 	util.AddNetworkString("lostHit")
	self:SetModel( "models/props/cs_office/phone.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )

	Hitlist = Hitlist or {}
 
        local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
	end
end
 
ENT.Once = false
function ENT:Use( activator, caller )

	if self.Once then return end

	self.Once = true
	timer.Simple(0.7, function() self.Once = false end)

    openHitMenu(activator,self, activator)
end
 

function ENT:Think()
    //
end


function openHitMenu(ply,ent)
	net.Start("openclHitMenu")
		net.WriteInt(ent:EntIndex(),4)
		net.WriteTable(Hitlist)
	net.Send(ply)
end

function addData(hit,client)
	Hitlist = Hitlist or {}

	client:AddMoney(-(hit.Price*0.75))

	local stack = {}
	for k,v in pairs(Hitlist) do
		if v.Player == hit.Player then
			hit.Price = hit.Price + v.Price
			table.insert(stack, k)
		end
	end
	for k,v in pairs(stack) do
		table.remove(Hitlist, v)
	end

	table.insert(Hitlist,hit)

	updateAllPlayers()
end


net.Receive("placeHit", function(f,client)  addData(net.ReadTable(),client) end)


function updateAllPlayers()
	for k,v in pairs(player.GetAll()) do
		net.Start("updateHitList")
			net.WriteTable(Hitlist)
		net.Send(v)
	end
end